package cn.wlinker.video.monitor.controller;

import cn.wlinker.video.monitor.domain.R;
import cn.wlinker.video.monitor.stream.Converter;
import cn.wlinker.video.monitor.stream.ConverterRegistration;
import cn.wlinker.video.monitor.stream.FlvServiceImpl;
import cn.wlinker.video.monitor.stream.IFlvService;
import cn.wlinker.video.monitor.utils.StreamUrlCacheUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;

/**
 * flv视频播放控制器
 *
 * @author gxsjx
 * @version 1.0
 * @date 2025/5/3
 * Copyright © wlinker.cn
 */
@CrossOrigin
@RestController
@RequestMapping("/flv")
@Api(tags = "视频播放")
@Slf4j
public class FlvVideoController {

    private final IFlvService iflvService = FlvServiceImpl.INSTANCE;

    /**
     * 根据视频rtsp流id打开转换，通过响应写出流到前台使用flv.js播放视频
     *
     * @param streamId            流id
     * @param httpServletResponse 响应请求
     * @author xufeng
     */
    @RequestMapping(method = RequestMethod.GET, value = "/live.flv")
    @ApiOperation(value = "通过流id播放视频")
    public void live(@RequestParam String streamId, HttpServletResponse httpServletResponse) {
        try {
            String url = StreamUrlCacheUtils.get(streamId);
            if (StringUtils.isBlank(url)) {
                log.error("视频流不存在");
                return;
            }
            iflvService.open(url, streamId, httpServletResponse);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 根据视频rtsp流链接打开转换，通过响应写出流到前台使用flv.js播放视频
     *
     * @param url                 视频链接
     * @param httpServletResponse 响应请求
     * @author xufeng
     */
    @RequestMapping(method = RequestMethod.GET, value = "/open.flv")
    @ApiOperation(value = "通过原始rtsp流地址播放视频")
    public void open(@RequestParam String url, HttpServletResponse httpServletResponse) {
        try {
            if (StringUtils.isBlank(url)) {
                log.error("url不能为空");
                return;
            }
            String streamId = Base64Utils.encodeToString(url.getBytes(StandardCharsets.UTF_8));
            iflvService.open(url, streamId, httpServletResponse);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 一个播放器销毁时，将对应转换器线程暂停
     *
     * @return EosDataTransferObject
     * @author xufeng
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/closeTransThread")
    @ApiOperation(value = "通过流id关闭视频")
    public R<String> closeTransThread(@RequestParam String streamId) {
        try {
            //通过视频流链接取对应的转换器
            Converter converter = ConverterRegistration.isExistStream(streamId);
            if (null != converter) {
                //暂停转换器线程，1分钟无新线程创建，该线程即被销毁
                converter.exit();
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return R.data("");
    }

}